<template>
  <div>
    <div :class="[$style.search_box, $style.topSelect]">
       <div :class="$style.selectBox">
        <label>套餐类型</label>
        <el-select
          style="width:160px"
          v-model="type"
          clearable
          @change="getQuery"
          placeholder="请选择类型"
        >
          <el-option label="总套餐" :value="1"></el-option>
          <el-option label="服务费套餐" :value="2"></el-option>
          <el-option label="电费套餐" :value="3"></el-option>
        </el-select>
      </div>
      <div :class="$style.search_box">
        <label>状态</label>
        <el-select
          style="width:160px"
          v-model="state"
          clearable
          @change="getQuery"
          placeholder="请选择状态"
        >
         <el-option label="未生效" :value="0"></el-option>
          <el-option label="生效中" :value="1"></el-option>
          <el-option label="过期" :value="2"></el-option>
          <el-option label="已使用完" :value="3"></el-option>
        </el-select>
      </div>
       <div :class="$style.search_box" style="margin-right:20px;display: flex;align-items: center;">
        <label>有效期：</label>
        <el-date-picker
          v-model="appointmentTime"
          type="daterange"
          @change="getQuery"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="timestamp"
          :picker-options="pickerOptions"
          style="height:40px"
        ></el-date-picker>
      </div>
    </div>
    <Panel title="用户套餐详情">
      <div slot='ext' :class="$style.examine">
        <!-- <div class="adopt" @click="()=>editStore()">新增</div> -->
    </div>
    <TableEncapsulation
      slot="body"
      :tableConfig="tableConfig"
      :tableData="tableData"
    >
    <template slot="operation" slot-scope="operation">
          <el-button
            type="text"
            @click="()=>editStore(operation.row.id)"
          >编辑</el-button>
          <el-button
            type="text"
            @click="()=>remove(operation.row.id)"
          >删除</el-button>
        </template>
        <TablePager
          slot="pager"
          v-if="pager.use"
          :size="pager.size"
          :pageChange="pageChange"
          :show-total="pager.showTotal"
          :total="pager.total"
          :current="pager.current"
        >
          <span class="demonstration">GO></span>
        </TablePager>
    </TableEncapsulation>
    </Panel>
    
  </div>
</template>
<script>
import request from "@/tools/request";
import Panel from "@/components/Panel";
import TableEncapsulation from "@/components/TableEncapsulation";
import TablePager from "@/components/Pager";
import { objectFilter } from "@/tools/array";
import { dateFormat } from "@/tools/time";
import SearchInput from "@/components/SearchInput";
export default {
  name: "zhandian",
  components: {
    TableEncapsulation,
    TablePager,
    Panel,
    SearchInput
  },
  data() {
    return {
      tableData: [],
      query: {
        page: 1
      },
       pager: {
        use: true,
        total: 0,
        current: 1,
        size: 10,
        showTotal: true
      },
      name: '',
      operation: true,
      state:'',
      type:'',
      appointmentTime: null,
      pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
    };
  },
  created() {
    this.getQuery();
  },
  computed: {
    tableConfig () {
      return {
        selection: false,
        columnConfig: [
        
          {
            label: "名称",
            type: "text",
            prop: "name",
            align: "center"
          },
          {
            label: "订单编号",
            type: "text",
            prop: "oid",
            align: "center"
          },
          {
            label: "用户昵称",
            type: "text",
            prop: "wxName",
            align: "center"
          },
          {
            label: "套餐电量",
            type: "text",
            prop: "electricity",
            align: "center"
          },
          {
            label: "剩余电量",
            type: "text",
            prop: "surplus",
            align: "center"
          },
          {
            label: "有效期(天)",
            type: "text",
            prop: "expire",
            align: "center"
          },
          {
            label: "开始时间",
            type: "text",
            prop: "beginTime",
            align: "center"
          },
          {
            label: "结束时间",
            type: "text",
            prop: "endTime",
            align: "center"
          },
         
          {
            label: "套餐类型",
            type: "other",
            prop: "type",
            align: "center",
            fnc: val => val == 1 ? '总套餐': val ==2 ? '服务费套餐': '电费套餐'
          },
          {
            label: "状态",
            type: "other",
            prop: "state",
            align: "center",
            fnc: val => val == 0 ? '未生效': val ==1 ? '生效中': val ==2 ? '过期' : '已使用'
          },
           
        //   this.operation && {
        //     type: "operation",
        //     align: "center",
        //     label: "操作",
        //     width:180
        //   }
        ].filter(Boolean)
        }
    }
  },
  methods: {
    async getQuery() {
        // return
      let endTime = this.appointmentTime ? (dateFormat(this.appointmentTime[1] + 86400000-1000, "yyyy-MM-dd HH:mm:ss")) : undefined
      let beginTime = this.appointmentTime ? (dateFormat(this.appointmentTime[0],"yyyy-MM-dd HH:mm:ss")) : undefined
      let state =  this.state
      let type = this.type
      let page = this.pager.current
      let search = objectFilter({beginTime, endTime, state, type, page})
      const res = await request({
        url: "/user/combo/list",
        method: "POST",
        data:  search
      });
      this.tableData = res.page.list
      this.pager.total = res.page.totalCount
      this.pager.current = res.page.currPage
    },
    pageChange(val) {
      this.pager.current = val;
      this.getQuery();
    },
    changeSearch(key, val) {
      this[key] = val;
      this.pager.current = 1;
    },
    editStore(id){
      this.$router.push({ path: "/combo/list/info",query:{id:id} })
    },
    commentDetail(id){
      this.$router.push({ path: "/storeDetail",query:{id:id,type: "toview"} })
    },
    remove(id){
       this.$confirm('确认删除吗', '操作确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then( async () => {
        let del = await request({
        url: "/combo/delete",
        method: "POST",
        dataType: 'json',
        data:  [id]
      });
      if (del.code !==0) return
        this.$message.success('删除成功')
        if(this.tableData.length===1 && this.pager.current>1)
          this.$set(this.pager,'current', this.pager.current-1)
        this.getQuery()
      })
    },
  }
}
</script>
<style lang='scss' module>
.examine{
    display: flex;
    align-items: center;
    :global{
        div{
            color: #fff;
            padding: 10px;
            margin: 0 10px;
            border-radius: 2px;
            font-size: 14px;
            cursor: pointer;
            min-width: 60px;
            text-align: center;
        }
        .adopt{
                background: #409EFF;
        }
    }
}
.selectBox {
  :global {
    label {
      font-size: 14px;
      color: #666666;
      margin-right: 20px;
    }
    .el-input__inner {
      height: 32px!important;
    }
    .el-input__suffix {
      line-height: 26px;
    }
    .el-date-editor--datetime {
      width: 200px;
    }
  }
}
.search_box {
  display: flex;
  align-items: center;
  flex-wrap: wrap;

  :global {
    .el-input {
      width: 150px;
    }
    .el-select {
      .el-input {
        width: 90px;
      }
    }
    .el-input__icon {
      line-height: 27px;
    }
    .el-range-separator{
      line-height: 26px;
    }
  }
}

.topSelect {
  background-color: white;
  border-radius: 4px;
  padding: 20px;
  margin-bottom: 20px;
}
</style>